package com.example.administrator.myapplication;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private DbHelper dbHelper;
    private String mName;
    private int  mAge;
    private String mSex;
    private String mClass;
    private List<Student> mList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        ListView listView = (ListView) findViewById(R.id.listView);
        dbHelper = new DbHelper(this);
        ListAdapter adapter = new ListAdapter(this,query());
        listView.setAdapter(adapter);

    }
    private List<Student> query(){
        //得到数据库对象
        SQLiteDatabase db = dbHelper.getWritableDatabase();

        Cursor cursor = db.rawQuery("select*from stu",null);

        mList = new ArrayList<>();
        while (cursor.moveToNext()){
            mName = cursor.getString(cursor.getColumnIndex("sname"));
            mAge = cursor.getInt(cursor.getColumnIndex("sage"));
            mSex = cursor.getString(cursor.getColumnIndex("ssex"));
            mClass = cursor.getString(cursor.getColumnIndex("sclass"));
            mList.add(new Student(mName,mAge,mSex,mClass));
        }
        return mList;
    }
}
